package com.yogee.topseller.activity.my;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AppManager;
import com.yogee.core.utils.SharedPreferencesUtils;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.view.impl.GuideLoginRegist;
import com.yogee.topseller.activity.subscription.bean.ProductAppointBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangeLoginPwdActivity extends HttpActivity {

    @BindView(R.id.title_white_left_back)
    LinearLayout titleWhiteLeftBack;
    @BindView(R.id.title_white_title)
    TextView titleWhiteTitle;
    @BindView(R.id.title_white_right_txt)
    TextView titleWhiteRightTxt;
    @BindView(R.id.cet_old_pwd)
    ClearEditText cetOldPwd;
    @BindView(R.id.iv_old_pwd_switch)
    ImageView ivOldPwdSwitch;
    @BindView(R.id.cet_new_pwd)
    ClearEditText cetNewPwd;
    @BindView(R.id.iv_new_pwd_switch)
    ImageView ivNewPwdSwitch;
    @BindView(R.id.cet_new_again_pwd)
    ClearEditText cetNewAgainPwd;
    @BindView(R.id.iv_new_pwd_again_switch)
    ImageView ivNewPwdAgainSwitch;

    boolean oldFlag = false ,newFlag = false ,newAgainFlag = false;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_login_pwd;
    }

    @Override
    protected void initView() {
        titleWhiteLeftBack.setVisibility(View.VISIBLE);
        titleWhiteTitle.setText("修改登录密码");
        titleWhiteRightTxt.setText("确认");
        titleWhiteRightTxt.setVisibility(View.VISIBLE);
    }

    private boolean checkView(){
        if (TextUtils.isEmpty(cetOldPwd.getText().toString())){
            ToastUtils.showToast(this,"请输入原密码");
            return false;
        } else if (TextUtils.isEmpty(cetNewPwd.getText().toString())){
            ToastUtils.showToast(this,"请输入新密码");
            return false;
        } else if(cetNewPwd.getText().toString().length()<6 || cetNewPwd.getText().toString().length()>20){
            ToastUtils.showToast(this,"请输入6-20位新密码");
            return false;
        } else if (TextUtils.isEmpty(cetNewAgainPwd.getText().toString())){
            ToastUtils.showToast(this,"请再次输入新密码");
            return false;
        } else if (!cetNewPwd.getText().toString().equals(cetNewAgainPwd.getText().toString())){
            ToastUtils.showToast(this,"两次输入新密码不一致");
            return false;
        }else if(cetOldPwd.getText().toString().equals(cetNewPwd.getText().toString())){
            ToastUtils.showToast(this,"新密码不能与原密码相同");
            return false;
        }
        return true;
    }

    private void changeLoginPwd(){
        HttpManager.getInstance().changeLoginPwd(AppUtil.getUserId(this),cetOldPwd.getText().toString(),cetNewPwd.getText().toString()).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ProductAppointBean>() {
            @Override
            public void onNext(ProductAppointBean o) {
                if ("1".equals(o.getResult())){
//                    ToastUtils.showToast(ChangeLoginPwdActivity.this,"密码修改成功");

                    AppUtil.clearUserInfo(ChangeLoginPwdActivity.this);
                    SharedPreferencesUtils.clear(ChangeLoginPwdActivity.this);
                    AppManager.finishAllActivity();
                    startActivity(new Intent(ChangeLoginPwdActivity.this, GuideLoginRegist.class).putExtra("isSetting", "1"));
                    finish();
                }
            }
        }, this));
    }
    
    @OnClick({R.id.title_white_left_back, R.id.title_white_right_txt, R.id.iv_old_pwd_switch, R.id.iv_new_pwd_switch, R.id.iv_new_pwd_again_switch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_white_left_back:
                if (AppUtil.isFastDoubleClick(500)){
                    return;
                }
                finish();
                break;
            case R.id.title_white_right_txt:
                if (checkView()){
                    changeLoginPwd();
                }
                break;
            case R.id.iv_old_pwd_switch:
                if (oldFlag){
                    //设置密文
                    cetOldPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    ivOldPwdSwitch.setBackgroundResource(R.mipmap.eye_icon);
                    cetOldPwd.setSelection(cetOldPwd.getText().toString().length());
                }else {
                    //设置明文
                    cetOldPwd.setInputType(InputType.TYPE_CLASS_TEXT);
                    ivOldPwdSwitch.setBackgroundResource(R.mipmap.eye_icon_show);
                    cetOldPwd.setSelection(cetOldPwd.getText().toString().length());
                }
                oldFlag = !oldFlag;
                break;
            case R.id.iv_new_pwd_switch:
                if (newFlag){
                    //设置密文
                    cetNewPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    ivNewPwdSwitch.setBackgroundResource(R.mipmap.eye_icon);
                    cetNewPwd.setSelection(cetNewPwd.getText().toString().length());
                }else {
                    //设置明文
                    cetNewPwd.setInputType(InputType.TYPE_CLASS_TEXT);
                    ivNewPwdSwitch.setBackgroundResource(R.mipmap.eye_icon_show);
                    cetNewPwd.setSelection(cetNewPwd.getText().toString().length());
                }
                newFlag = !newFlag;
                break;
            case R.id.iv_new_pwd_again_switch:
                if (newAgainFlag){
                    //设置密文
                    cetNewAgainPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    ivNewPwdAgainSwitch.setBackgroundResource(R.mipmap.eye_icon);
                    cetNewAgainPwd.setSelection(cetNewAgainPwd.getText().toString().length());
                }else {
                    //设置明文
                    cetNewAgainPwd.setInputType(InputType.TYPE_CLASS_TEXT);
                    ivNewPwdAgainSwitch.setBackgroundResource(R.mipmap.eye_icon_show);
                    cetNewAgainPwd.setSelection(cetNewAgainPwd.getText().toString().length());
                }
                newAgainFlag = !newAgainFlag;
                break;
        }
    }
}
